package com.situation.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situation.mapper.BizProductCategoryMapper;
import com.situation.mapper.BizProductMapper;
import com.situation.pojo.DO.BizProduct;
import com.situation.pojo.DO.BizProductCategory;
import com.situation.service.BizProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BizProductCategoryServiceImp implements BizProductCategoryService {
    @Autowired
    private BizProductMapper productMapper;
    @Autowired
    private BizProductCategoryMapper bizMapper;

    @Override
    public int add(BizProductCategory bizProductCategory) {
        return bizMapper.add(bizProductCategory);
    }

    @Override
    public BizProductCategory selectById(Long id) {
        return bizMapper.selectById(id);
    }

    @Override
    public List<BizProductCategory> groupById(Long pid) {
        return bizMapper.groupById(pid);
    }

    @Override
    public PageInfo<BizProductCategory> findAll() {
        PageHelper.startPage(1, 10);
        List<BizProductCategory> all = bizMapper.findAll();
        return new PageInfo<>(all);
    }

    @Override
    public PageInfo<BizProductCategory> findBizProductCategoryList(BizProductCategory bizProductCategory, int pageNum, int size) {
        PageHelper.startPage(pageNum, size);
        List<BizProductCategory> bizProductCategoryList = bizMapper.findBizProductCategoryList(bizProductCategory);
        return new PageInfo<>(bizProductCategoryList);
    }

    @Override
    public int update(BizProductCategory bizProductCategory) {
        return bizMapper.update(bizProductCategory);
    }

    @Override
    public PageInfo<BizProductCategory> getcategoryTree(int pageNUm, int size) {
        PageHelper.startPage(pageNUm, size);
        List<BizProductCategory> bizProductCategories = bizMapper.groupById(0L);
        for (BizProductCategory bizProductCategory1 : bizProductCategories) {
            List<BizProductCategory> bizProductCategories2 = bizMapper.groupById(bizProductCategory1.getId());
            for (BizProductCategory bizProductCategory3 : bizProductCategories2) {
                List<BizProductCategory> bizProductCategories4 = bizMapper.groupById(bizProductCategory3.getId());
                bizProductCategory3.setChildren(bizProductCategories4);
            }
            bizProductCategory1.setChildren(bizProductCategories2);
        }
        return new PageInfo<>(bizProductCategories);
    }

    @Override
    public List<BizProductCategory> getParentCategoryTree() {
        List<BizProductCategory> bizProductCategories = bizMapper.groupById(0L);
        for (BizProductCategory bizProductCategory : bizProductCategories) {
            List<BizProductCategory> bizProductCategories1 = bizMapper.groupById(bizProductCategory.getId());
            bizProductCategory.setChildren(bizProductCategories1);
        }
        return bizProductCategories;
    }

    @Override
    public int delete(Long id) {
        int a = isDelete(id);
        if (a == 4) {
            return bizMapper.delete(id);
        } else
            return a;
    }


    @Override
    public int isDelete(Long id) {
        List<BizProductCategory> bizProductCategories = bizMapper.groupById(id);
        for (BizProductCategory bizProductCategory : bizProductCategories) {
            if (bizProductCategory.getName() != null) {
                return 3;
            }
        }
        List<String> pNums = productMapper.findProductByThreeId(id);
        for (String pNum : pNums) {
            Long stockBypNum = productMapper.getStockBypNum(pNum);
            if (stockBypNum > 0) {
                return 2;
            }
        }
        return 4;
    }


}
